//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
#include <elasys_server.h>
#include "wincetypes.h"
#include <driver.h>
#include "calibrate.h"
#include <mouse.h>
#include <stdio.h>

BOOL SetCalibrate(RefPoint *P);
void InitMouse();
void DeleMouse();

Mouse::Mouse()
{
    InitMouse();
}

ECode Mouse::Read(
    /* [in] */ Int64 u64Offset,
    /* [in] */ Int32 bytesToRead,
    /* [out] */ MemoryBuf * pBuffer,
    /* [out] */ IEvent * * ppCompletionEvent)
{
    if (pBuffer == NULL ||
        pBuffer->GetCapacity() < (int)sizeof(TOUCH_DATA)) {
        return E_INVALID_ARGUMENT;
    }
    return g_pEventQueues->Read(EVENT_TOUCH,
        bytesToRead, pBuffer, ppCompletionEvent);
}

ECode Mouse::Write(
    /* [in] */ Int64 u64Offset,
    /* [in] */ const MemoryBuf & buffer,
    /* [out] */ Int32 * pBytesWritten,
    /* [out] */ IEvent * * ppCompletionEvent)
{
    return E_NOT_IMPLEMENTED;
}

ECode Mouse::Control(
    /* [in] */ Handle32 nControlCode,
    /* [in] */ const MemoryBuf & inBuffer,
    /* [out] */ MemoryBuf * pOutBuffer,
    /* [out] */ IEvent * * ppCompletionEvent)
{
    ECode ec = E_ACCESS_DENIED;
    switch (nControlCode) {
        case 2:{
            int ret = SetCalibrationMatrix(5, (RefPoint *)inBuffer.GetPayload());
            if (0 == ret) ec = NOERROR;
            break;
        }
        case 3:{
            RefPoint P;
            if (SetCalibrate(&P)) {
                assert(pOutBuffer && pOutBuffer->GetCapacity() >= (int)sizeof(RefPoint));
                pOutBuffer->Copy((Byte *)&P, sizeof(RefPoint));
                ec = NOERROR;
            }
            printf(">>>>>>>>return %x, Got Raw Coordinate (%d, %d)\n", ec, P.x, P.y);
            break;
        }
        default:
            break;
    }
    return ec;
}

void Mouse::Dispose()
{
    DeleMouse();
    delete this;
}

IInterface * CreateTouch()
{
    static Mouse sMouse;
    return (IInterface *)&sMouse;
}
